package com.jinxiangyang.controller;

import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.service.VodService;
import com.jinxiangyang.utils.ConstantPropertiesUtil;
import com.jinxiangyang.utils.Signature;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Random;

/**
 * ClassName:VodController
 * Description:
 *
 * @Author: jiangcheng
 * @Create: 2023/8/21 - 14:29
 * @Version: v1.0
 */

@Api(tags = "腾讯云点播")
@RestController
@RequestMapping("/admin/vod")
@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;
    @GetMapping("sing")
    @OperateLog(title = "腾讯云点播", operatorType = OperatorType.STUDENT, businessType = BusinessType.OTHER)
    public AjaxResult sign() {
        Signature sign = new Signature();
        // 设置 App 的云 API 密钥
        sign.setSecretId(ConstantPropertiesUtil.ACCESS_KEY_ID);
        sign.setSecretKey(ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        sign.setCurrentTime(System.currentTimeMillis() / 1000);
        sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
        sign.setSignValidDuration(3600 * 24 * 2); // 签名有效期：2天
        try {
            String signature = sign.getUploadSignature();
            System.out.println("signature : " + signature);
            return AjaxResult.success(signature);
        } catch (Exception e) {

            System.out.print("获取签名失败");
            e.printStackTrace();
            return AjaxResult.error("获取签名失败");
        }

    }

    @PostMapping("upload")
    @OperateLog(title = "腾讯云点播", operatorType = OperatorType.STUDENT, businessType = BusinessType.OTHER)
    public AjaxResult upload(
                             @ApiParam(name = "file", value = "文件", required = true)
                             @RequestParam("file") MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();

        String videoId = vodService.uploadVideo(inputStream, originalFilename);
        return AjaxResult.success(videoId);
    }


    @OperateLog(title = "腾讯云点播", operatorType = OperatorType.STUDENT, businessType = BusinessType.OTHER)
    @DeleteMapping("remove/{fileId}")
    public AjaxResult delete(@PathVariable Long fileId){
        vodService.removeVideo(fileId);
        return new AjaxResult();
    }
}
